// pages/authentica/companyauthentica.js
import config from '~/configs/configs'
import baseuser from '~/utils/users.js'
import baseteam from '~/utils/teamleader.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        name: '',
        headTouchPerson: '',
        headTouchPhone: '',
        show: false,
        pingtai: false,
        pingtaicolumns: ['杭州', '宁波', '温州', '嘉兴', '湖州'],
        pingtaiselect: '',
        zhiying: false,
        zhiyingcolumns: ['暂无数据'],
        zhiyingselect: '',
        filezhuList: [],
        filebangList: [],
        agrchecked: true,
    },
    openpingtaiactive() {
        this.setData({ pingtai: true })
    },
    openzhiyingactive() {
        this.setData({ zhiying: true })
    },
    naveback(){
        setTimeout(() => {
            wx.navigateBack()
        }, 300);
    },
    onChange(event) {
        this.setData({
            agrchecked: event.detail,
        });
    },
    async nextnave() {
        if (this.data.agrchecked == false){
            wx.showToast({
                title: '未同意协议',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (this.data.name == '') {
            wx.showToast({
                title: '昵称还未输入',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (this.data.zhiyingselect === '') {
            wx.showToast({
                title: '主营类目还未选择',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (this.data.headTouchPerson == '') {
            wx.showToast({
                title: '联系人还未输入',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (this.data.headTouchPhone == '') {
            wx.showToast({
                title: '联系方式还未输入',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        wx.showLoading({
            title: '上传中, 请稍等..',
            mask: true
        })
        let datalistzhu = []
        if (this.data.filezhuList.length != 0) {
            datalistzhu = await this.filesUpload(this.data.filezhuList)
        }
        let datalistbang = []
        if (this.data.filebangList.length != 0) {
            datalistbang = await this.filesUpload(this.data.filebangList)
        }

        console.log(datalistzhu)
        console.log(datalistbang)
        let setdata = {
            "name": this.data.name,
            "headMainCategory": this.data.zhiyingcolumns[this.data.zhiyingselect].name,
            "headTouchPerson": this.data.headTouchPerson,
            "headTouchPhone": this.data.headTouchPhone,
            "headHomepageImgList": datalistzhu,
            "headHelpCodeList": datalistbang
        }
        let addtime = await baseteam.applytime(setdata)
        if (addtime.code == 200) {
            wx.hideLoading()
            wx.showToast({
                title: '上传成功',
                icon: 'success',
                duration: 2000
            })
            this.setData({
                show:true
            })

        } else {
            wx.hideLoading()
            wx.showToast({
                title: '上传失败',
                icon: 'error',
                duration: 2000
            })
        }

        // this.setData({ show: true });
    },

    onClose() {
        this.setData({
            show: false,
            pingtai: false,
            zhiying: false
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getzhuying()
    },
    async getzhuying() {
        let reqdata = await baseteam.zhuyinglist()
        let zhiyingcolumns = reqdata.data.result
        this.setData({
            zhiyingcolumns
        })
    },
    onConfirmpingtai(event) {
        let { picker, value, index } = event.detail;
        console.log(picker, value, index)
        setTimeout(() => {
            this.onClose()
        }, 300);
    },
    onConfirmzhiying(event) {
        let { picker, value, index } = event.detail;
        console.log(picker, value, index)
        this.setData({
            zhiyingselect: index
        })
        setTimeout(() => {
            this.onClose()
        }, 300);
    },
    bangafterReadzhu(event) {
        // console.log(config.baseUrl)
        // console.log(baseuser.adminossfileupload)
        const { file } = event.detail;
        const { filezhuList = [] } = this.data;
        filezhuList.push(...file);
        this.setData({ filezhuList });

        // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
        // wx.uploadFile({
        //     url: config.baseUrl+baseuser.adminossfileupload, // 仅为示例，非真实的接口地址
        //     filePath: file.url,
        //     name: 'file',
        //     success(res) {
        //         // 上传完成需要更新 fileList
        //         console.log(res.data)

        //     },
        // });
    },
    bangdel(event) {
        let filezhuList = this.data.filezhuList;
        filezhuList.splice(event.detail.index, 1)
        this.setData({ filezhuList });
    },
    bangafterReadbang(event) {
        // console.log(config.baseUrl)
        // console.log(baseuser.adminossfileupload)
        const { file } = event.detail;
        const { filebangList = [] } = this.data;
        filebangList.push(...file);
        this.setData({ filebangList });

        // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
        // wx.uploadFile({
        //     url: config.baseUrl+baseuser.adminossfileupload, // 仅为示例，非真实的接口地址
        //     filePath: file.url,
        //     name: 'file',
        //     success(res) {
        //         // 上传完成需要更新 fileList
        //         console.log(res.data)

        //     },
        // });
    },
    bangdelbang(event) {
        let filebangList = this.data.filebangList;
        filebangList.splice(event.detail.index, 1)
        this.setData({ filebangList });
    },

    filesUpload(paths) {
        return new Promise((resolve, reject) => {
            // wx.showLoading({
            //     title: '上传中, 请稍等..',
            //     mask: true
            // })
            let uploadCount = 0; // 上传计数
            const resources = [] // 上传结果
            //上传图片
            for (const path of (Array.isArray(paths) ? paths : [paths])) {
                wx.uploadFile({
                    url: config.baseUrl + baseuser.adminossfileupload,
                    filePath: path.url,
                    name: 'file',
                    timeout: 60 * 1000,
                    success: function (res) {
                        let data = JSON.parse(res.data);
                        if (data.code == 200) {
                            resources.push(data.data.map.url)
                        }
                    },
                    complete: () => {
                        uploadCount++
                        if (resources.length == (Array.isArray(paths) ? paths.length : 1)) {
                            // wx.hideLoading()
                            if (resources.length == 0) {
                                // showToast('上传失败')
                                reject('上传失败')
                            } else {
                                // showToast(paths instanceof String ? '上传成功' : '成功上传：' + resources.length + '张')
                                resolve(resources)
                            }
                        }
                    }
                });
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})